/**
 * 
 */
package com.base.myproject.client.busi.service.analysis;

import java.util.Date;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.model.BarDataProvider;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.Legend;
import com.extjs.gxt.charts.client.model.LineDataProvider;
import com.extjs.gxt.charts.client.model.ScaleProvider;
import com.extjs.gxt.charts.client.model.Text;
import com.extjs.gxt.charts.client.model.Legend.Position;
import com.extjs.gxt.charts.client.model.charts.BarChart;
import com.extjs.gxt.charts.client.model.charts.LineChart;
import com.extjs.gxt.charts.client.model.charts.BarChart.BarStyle;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.google.gwt.http.client.URL;

/**
 * 周营业额对比图
 * @author zgw
 *2009-11-15 上午08:59:47
 * javazgw@gmail.com
 */
public class WeekSaleAnalysis extends ContentPanel{



	ChartModel model =null;
	BaseStore<JDBCModelData> store;
	
	SelectionListener<ButtonEvent> listener ;
	
	int week=0;
	public WeekSaleAnalysis() {
		init();
	}
	void init() {
		Date d = new Date();
		
		setWidth("400px");
		initlistener();
		initstore();
		initQueryParamPanel();
		initWidget();
		this.setAutoHeight(true);
	}
	void initstore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		// type.addField("b2cgcode");
		type.addField("paid");

		type.addField("week");
	

		StoreInfo info = new StoreInfo();


		info.setTablename("sleeping");
		info.setNeedAllRow("no");
		// info.setSql("");
		store = new BaseStore<JDBCModelData>(type, info,false);

	}

	void initWidget() {
		 model = new ChartModel("周营业额分析",
				"font-size: 16px; font-family: Verdana; text-align: center;");
		model.setBackgroundColour("#fefefe");
		model.setLegend(new Legend(Position.TOP, true));
		model.setScaleProvider(ScaleProvider.ROUNDED_NEAREST_SCALE_PROVIDER);

		
		 BarChart bar = new BarChart(BarStyle.GLASS);
		  bar.setColour("#00aa00"); 
		  BarDataProvider barProvider = new	  BarDataProvider( "paid","week");
		  barProvider.bind(store);
		 bar.setDataProvider(barProvider); 
		 //bar.addChartListener(listener);
		 model.addChartConfig(bar);
		 
		 LineChart line = new LineChart();
			line.setAnimateOnShow(true);
			line.setText("营业额");
			line.setColour("#ff6600");
			LineDataProvider lineProvider = new LineDataProvider("paid","week");
			lineProvider.bind(store);
			line.setDataProvider(lineProvider);
			model.addChartConfig(line);
		 
		 
		 String url = "chart/open-flash-chart.swf";
		 final Chart chart = new Chart(url);
			// chart.setToolTip("点击产品的图型看看");
			chart.setChartModel(model);
			chart.setHeight("300px");
			add(chart);
	}
	
	void initQueryParamPanel() {
		Button preweek = new Button("上一周");
		preweek.addSelectionListener(listener);
		Button nextweek = new Button("下一周");
		nextweek.addSelectionListener(listener);
		Button thisweek = new Button("本周");
		thisweek.addSelectionListener(listener);
		this.addButton(preweek);
		this.addButton(thisweek);
		this.addButton(nextweek);
		
	}
	 void initlistener()
	 {
		 listener = new SelectionListener<ButtonEvent>()
		 {
				public void componentSelected(ButtonEvent ce) {
					// TODO Auto-generated method stub
					if(ce.getButton().getText().equals("上一周"))
					{
						week--;
						
						model.setTitle(new Text("第"+week +""+"周营业额分析"));
					}
					else if(ce.getButton().getText().equals("下一周"))
					{
						week++;
						model.setTitle(new Text("第"+week +""+"周营业额分析"));
					}
						else if(ce.getButton().getText().equals("本周"))
						{
							week=0;
							model.setTitle(new Text("本周"+"周营业额分析"));
						}
					//if(ce.getButton().getText().equals("本周"))
					{
						
						StoreInfo info = store.getStoreinfo();
						String str = " select * from (";
						str += "select sum(isnull(paid,0)) as paid,'"+URL.encode("周日")+"'  as week ,0 as d from s_service_po where  datepart(week,createdate)=datepart(week,getdate())%2B"+week+" and datepart(weekday,createdate)=1";
						str += " union ";
						str += "select sum(isnull(paid,0)) as paid,'"+URL.encode("周一")+"' as week,1 as d from s_service_po where  datepart(week,createdate)=datepart(week,getdate())%2B"+week+" and datepart(weekday,createdate)=2 ";
						str += " union ";
						str += "select sum(isnull(paid,0)) as paid,'"+URL.encode("周二")+"' as week,2 as d from s_service_po where  datepart(week,createdate)=datepart(week,getdate())%2B"+week+" and datepart(weekday,createdate)=3";
						str += " union ";
						str += "select sum(isnull(paid,0)) as paid,'"+URL.encode("周三")+"' as week,3 as d from s_service_po where  datepart(week,createdate)=datepart(week,getdate())%2B"+week+" and datepart(weekday,createdate)=4 ";
						str += " union ";
						str += "select sum(isnull(paid,0)) as paid,'"+URL.encode("周四")+"' as week,4 as d from s_service_po where  datepart(week,createdate)=datepart(week,getdate())%2B"+week+" and datepart(weekday,createdate)=5";
						str += " union ";
						str += "select sum(isnull(paid,0)) as paid,'"+URL.encode("周五")+"' as week,5 as d from s_service_po where  datepart(week,createdate)=datepart(week,getdate())%2B"+week+" and datepart(weekday,createdate)=6 ";
						str += " union ";
						str += "select sum(isnull(paid,0)) as paid,'"+URL.encode("周六")+"' as week,6 as d from s_service_po where  datepart(week,createdate)=datepart(week,getdate())%2B"+week+" and datepart(weekday,createdate)=7 ";
						str += " ) as a order by d ";
						info.setSql(str);
						store.reload();
						
						
					}
				}
		 };
		 
	 }
}
